package com.cy.store.controller;

import com.cy.store.service.ICartService;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController{

    @Autowired
    ICartService cartService;

    @RequestMapping("add_to_cart")
    JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session){
        cartService.addToCart(getuidFromSession(session), pid, amount, getUsernameFromSession(session));

        return new JsonResult<>(OK);
    }


    /**
     * 展示某个用户 的收藏商品
     */
    @RequestMapping({"", "/"})
    JsonResult<List<CartVO>> getVOByUid(HttpSession session){
        List<CartVO> cartVOS = cartService.getVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK, cartVOS);
    }


    /**
     * 修改收藏数量
     * @param cid 收藏品的id
     * @param n 修改数量
     * @return 修改后的数量
     */
    @RequestMapping("{cid}/num/alter/{n}")
    JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session, @PathVariable("n") Integer n){

        Integer data = cartService.addNum(cid, getUsernameFromSession(session), getuidFromSession(session), n);
        return new JsonResult<>(OK, data);
    }



    /**
     * 结算
     */
    @RequestMapping("list")
    JsonResult<List<CartVO>> getVOByCid(Integer[] cids, HttpSession session){
        List<CartVO> data = cartService.getVOByCid(cids, getuidFromSession(session));
        return new JsonResult<>(OK, data);
    }
}
